package com.cs.bd.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Build.VERSION;
import java.net.NetworkInterface;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;

public class NetworkUtils
{
    private static final String LOG_TAG = "appcenter_network";
    public static final int NETWORK_TYPE_IDEN = 11;
    public static final int NETWORK_TYPE_EVDO_B = 12;
    public static final int NETWORK_TYPE_LTE = 13;
    public static final int NETWORK_TYPE_EHRPD = 14;
    public static final int NETWORK_TYPE_HSPAP = 15;
    public static final int TYPE_MOBILE_MMS = 2;
    public static final int TYPE_MOBILE_SUPL = 3;
    public static final int TYPE_MOBILE_DUN = 4;
    public static final int TYPE_MOBILE_HIPRI = 5;
    public static final int TYPE_WIMAX = 6;
    public static final int TYPE_BLUETOOTH = 7;
    public static final int TYPE_DUMMY = 8;
    public static final int TYPE_ETHERNET = 9;
    public static final int NETWORK_TYPE_UNKOWN = 0;
    public static final int NETWORK_TYPE_WIFI = 1;
    public static final int NETWORK_TYPE_2G = 2;
    public static final int NETWORK_TYPE_3G = 3;
    public static final int NETWORK_TYPE_4G = 4;
    public static final int NETWORK_TYPE_OTHER = 5;

    public static boolean isNetworkOK(Context context)
    {
        boolean result = false;
        if (context != null) {
            try
            {
                ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
                if (cm != null)
                {
                    NetworkInfo networkInfo = cm.getActiveNetworkInfo();
                    if ((networkInfo != null) && (networkInfo.isConnected())) {
                        result = true;
                    }
                }
            }
            catch (NoSuchFieldError e)
            {
                e.printStackTrace();
            }
        }
        return result;
    }

    public static boolean isWifiEnable(Context context)
    {
        if (context == null) {
            return false;
        }
        ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetInfo = connectivityManager != null ? connectivityManager.getActiveNetworkInfo() : null;
        if ((activeNetInfo != null) && (activeNetInfo.isConnected()) && (activeNetInfo.getType() == 1)) {
            return true;
        }
        return false;
    }

    public static int getNetworkType(Context context)
    {
        int type = 0;
        ConnectivityManager connectMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = connectMgr.getActiveNetworkInfo();
        if (info != null)
        {
            switch (info.getType())
            {
                case 1:
                    type = 1;
                    break;
                case 0:
                    switch (info.getSubtype())
                    {
                        case 1:
                        case 2:
                        case 4:
                        case 7:
                            type = 2;
                            break;
                        case 3:
                        case 5:
                        case 6:
                        case 8:
                        case 9:
                        case 10:
                        case 11:
                        case 12:
                        case 14:
                        case 15:
                            type = 3;
                            break;
                        case 13:
                            type = 4;
                            break;
                        case 0:
                            type = 0;
                            break;
                        default:
                            type = 0;
                    }
                    break;
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                case 7:
                case 8:
                case 9:
                    type = 5;
                    break;
                default:
                    type = 0;
            }
        }
        return type;
    }

    public static String buildNetworkState(Context context)
    {
        String ret = "unknown";

        int networkType = getNetworkType(context);
        if (networkType == 0) {
            ret = "unknown";
        } else if (networkType == 1) {
            ret = "wifi";
        } else if (networkType == 2) {
            ret = "2g";
        } else if (networkType == 3) {
            ret = "3g";
        } else if (networkType == 4) {
            ret = "4g";
        } else if (networkType == 5) {
            ret = "other";
        }
        return ret;
    }

    public static boolean isVpnConnected()
    {
        if (Build.VERSION.SDK_INT >= 9) {
            try
            {
                Enumeration<NetworkInterface> niList = NetworkInterface.getNetworkInterfaces();
                if (niList != null) {
                    for (NetworkInterface intf : Collections.list(niList)) {
                        if ((intf.isUp()) && (intf.getInterfaceAddresses().size() != 0)) {
                            if (("tun0".equals(intf.getName())) || ("ppp0".equals(intf.getName()))) {
                                return true;
                            }
                        }
                    }
                }
            }
            catch (Throwable e)
            {
                e.printStackTrace();
            }
        }
        return false;
    }
}
